package com.example.a79273.shandianban.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.StyleActivity;
import com.example.a79273.shandianban.adapter.StyleVpAdapter;
import com.example.a79273.shandianban.config.NetconfigStyle;
import com.example.a79273.shandianban.model.StyleBean;
import com.example.a79273.shandianban.tools.Flyformer;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 79273 on 2016/8/29.
 */
public class YouXing extends Fragment implements View.OnClickListener{

    private View view, itemView;
    private ViewPager vp_fragment_style_body;
    private ImageView iv_fragment_style_share, iv_fragment_style_body,
            iv_style_mr2, iv_style_mr1;
    private TextView tv_fragment_style_price, tv_fragment_style_name;
    private StyleVpAdapter styleVpAdapter;
    private String path;
    private List<ImageView> mData;
    private StyleBean styleBean;
    private RadioGroup rg_fragment_style_body;
    private RadioButton rb_fragment_style_no;
    private LinearLayout rl_style_rg;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
            container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_style, null);
        initView();
        initData();
        setData();
        setListener();
        return view;
    }


    private void setListener() {
        vp_fragment_style_body.setOnClickListener(this);
        vp_fragment_style_body.addOnPageChangeListener(new ViewPager
                .OnPageChangeListener() {


            @Override
            public void onPageScrolled(int position, float positionOffset, int
                    positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position % mData.size() == 0) {
                    position = 0;
                    vp_fragment_style_body.setCurrentItem(0);
                } else if (position % mData.size() == 1) {
                    position = 1;
                }
                StyleBean.DataBean.ListsBean listsBean = styleBean.getData()
                        .getLists().get(position);
                String pic_title = listsBean.getPic_title();
                double pic_price = listsBean.getPic_price();
                tv_fragment_style_name.setText(pic_title);
                tv_fragment_style_price.setText("￥" + pic_price + "");
                for (int i = 0; i <mData.size() ; i++) {
                    RadioButton childAt = (RadioButton) rl_style_rg
                            .getChildAt(i);
                    childAt.setChecked(false);
                }
                RadioButton radioButton = (RadioButton) rl_style_rg
                        .getChildAt(position);
                radioButton.setChecked(true);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void setData() {

        RequestParams params = new RequestParams(path);
        x.http().get(params, new Callback.CommonCallback<String>() {


            @Override
            public void onSuccess(String result) {
                vp_fragment_style_body.setVisibility(View.VISIBLE);
                Gson gson = new Gson();
                int start = result.indexOf("[");
                int end = result.lastIndexOf("]");
                String substring = result.substring(start + 1, end);
                styleBean = gson.fromJson(substring, StyleBean.class);
                List<StyleBean.DataBean.ListsBean> lists = styleBean.getData()
                        .getLists();
                StyleBean.DataBean.ListsBean listsBean1 = lists.get(0);
                String pic_title = listsBean1.getPic_title();
                double pic_price = listsBean1.getPic_price();
                tv_fragment_style_name.setText(pic_title);
                tv_fragment_style_price.setText("￥" + pic_price + "");
                for (int i = 0; i < lists.size(); i++) {
                    final StyleBean.DataBean.ListsBean listsBean = lists.get(i);
                    String pic_url = listsBean.getPic_scr();
                    String pic = NetconfigStyle.PIN_JIE + pic_url;
                    ImageView imageView = new ImageView(getActivity());
                    Picasso.with(getActivity()).load(pic).into(imageView);
                    mData.add(imageView);
                    itemView = LayoutInflater.from(getActivity()).inflate(R
                            .layout.item_style_radiubutton, null);
                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getActivity(),StyleActivity.class);
                            String pic_id = listsBean.getPic_id();
                            String htmldetail = listsBean.getHtmldetail();
                            intent.putExtra("html",htmldetail);
                            intent.putExtra("id",pic_id);
                            startActivity(intent);
                        }
                    });
                    rb_fragment_style_no = (RadioButton) itemView.findViewById
                            (R.id.rb_fragment_style_no);

                    rb_fragment_style_no.setLayoutParams(new ViewGroup
                            .LayoutParams(8,8));
                    rl_style_rg.addView(itemView);
                    rb_fragment_style_no.setChecked(false);

                }
                RadioButton radioButton = (RadioButton) rl_style_rg
                        .getChildAt(0);
                radioButton.setChecked(true);
                styleVpAdapter = new StyleVpAdapter(mData);
                vp_fragment_style_body.setAdapter(styleVpAdapter);
                vp_fragment_style_body.setPageTransformer(true, new Flyformer());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initData() {
        path = NetconfigStyle.STYLE_PATH;
        mData = new ArrayList<>();

    }

    private void initView() {
        tv_fragment_style_price = (TextView) view.findViewById(R.id
                .tv_fragment_style_price);
        iv_fragment_style_share = (ImageView) view.findViewById(R.id
                .iv_fragment_style_share);
        tv_fragment_style_name = (TextView) view.findViewById(R.id
                .tv_fragment_style_name);
        vp_fragment_style_body = (ViewPager) view.findViewById(R.id
                .vp_fragment_style_body);
        rb_fragment_style_no = (RadioButton) view.findViewById(R.id
                .rb_fragment_style_no);
        rg_fragment_style_body= (RadioGroup) view.findViewById(R.id.rg_fragment_style_body);
        rl_style_rg = (LinearLayout) view.findViewById(R.id.rl_style_rg);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_fragment_style_share:

                break;
        }
    }
}
